#include <iostream>
#include <unistd.h>
#include <pthread.h>

// 线程函数的传参
class PthreadData
{
public:
    PthreadData(const std::string name = nullptr, int a = 0, int b = 0)
        : _name(name),
          _a(a),
          _b(b)
    {
    }
    int Excute()
    {
        return _a + _b;
    }
    std::string Name()
    {
        return _name;
    }
    int A() { return _a; }
    int B() { return _b; }
    ~PthreadData()
    {
    }

private:
    std::string _name;
    int _a;
    int _b;
};
void *func1(void *data)
{
    PthreadData *D = static_cast<PthreadData *>(data);
    while (true)
    {
        std::cout << "子进程:" << D->Name() << " " << D->A() << "+" << D->B() << "=" << D->Excute() << std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t th1;

    // 不推荐这样写,因为线程之间的栈区共享,可能会被其他线程修改
    // PthreadData data1(10,20,"pthread-1")
    // pthread_create(&th1,nullptr,func1,(void*)&data1);

    // 最好是在堆上申请空间,因为堆区不共享(理解上)
    PthreadData *data1 = new PthreadData("pthread-1", 10, 20);
    pthread_create(&th1, nullptr, func1, (void *)data1);
    pthread_join(th1, nullptr);
    return 0;
}